package com.guitest.gui.jframe;

import com.guitest.gui.entity.User;
import com.guitest.gui.sqlutils.Insert;
import com.guitest.gui.userinfo.UserCode;
import com.guitest.gui.userinfo.UserTem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class MyDialogDemo extends JDialog {
    private String username;
    private String userCode;
    private double tem;

    public MyDialogDemo(){
        this.setResizable(false);
        this.setTitle("测温者信息输入");
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        //容器
        Container container = this.getContentPane();
        //container.setLayout(null);

        //创建元素
        JLabel label01 = new JLabel("测温者姓名:");
        JLabel label02 = new JLabel("测温者信息码:");
        LimitNumTextField textField01 = new LimitNumTextField(10);//测温者姓名输入框
        JButton button01 = new JButton(">点击生成信息码<");
        JButton button02 = new JButton("开始测温");

        //布局
        JPanel panel01 = new JPanel(new GridLayout(1,2));
        JPanel panel02 = new JPanel(new GridLayout(1,2));
        JPanel panel03 = new JPanel(new GridLayout(3,1,10,10));
        label01.setBounds(30,30,80,50);
        label02.setBounds(30,60,80,50);
        label01.setHorizontalAlignment(SwingConstants.CENTER);//label居中显示
        label02.setHorizontalAlignment(SwingConstants.CENTER);
        textField01.setBounds(30,30,100,50);
        button01.setBounds(30,30,200,50);

        panel01.setBounds(30,30,200,100);
        panel02.setBounds(30,60,200,100);

        //事件
        //MyActionListener01 myActionListener01 = new MyActionListener01();//输入框回车事件
        //textField01.addActionListener(myActionListener01);
        textField01.addFocusListener(new MyActionListener03());//输入框失去焦点事件
        //点击按钮，生成信息码
        button01.addActionListener(new MyActionListener02());//测温者信息码按钮事件
        //开始测温按钮事件
        button02.addActionListener(new MyActionListener04());

        //先将所有元素放在不同的面板（JPanel）中
        button01.setBackground(new Color(255, 252, 172));
        button02.setBackground(new Color(255, 156, 1));
        panel01.setBackground(new Color(189, 245, 255));
        panel02.setBackground(new Color(189, 245, 255));
        panel03.setBackground(new Color(6, 237, 255));
        panel01.add(label01);
        panel01.add(textField01);
        panel02.add(label02);
        panel02.add(button01);

        panel03.add(panel01);
        panel03.add(panel02);
        panel03.add(button02);

        //最后再将JPanel放到容器Container中
        container.add(panel03);
    }

    //内部类，01 输入框事件（失去焦点）
    private class MyActionListener03 implements FocusListener {
        @Override
        public void focusGained(FocusEvent focusEvent) {

        }

        @Override
        public void focusLost(FocusEvent focusEvent) {
            JTextField field = (JTextField) focusEvent.getSource();
            username = field.getText();
            if (username.equals("")){
                System.out.println("姓名不能为空");
                //姓名为空时的警告弹窗
                new MyDialogDemo02();
            }
            else{
                System.out.println("测温者姓名:" + username);
            }
        }
    }

    //内部类，02 测温者信息码按钮事件
    class MyActionListener02 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            if (username.equals("")){
                System.out.println("姓名不能为空");
                //姓名为空时的警告弹窗
                new MyDialogDemo02();
            }
            else{
                UserCode oneUserCode = new UserCode();
                userCode = oneUserCode.getRandomNickname();
                System.out.println("6位信息码:" + userCode);
            }
        }
    }

    //内部类，03 姓名为空时的警告弹窗
    class MyDialogDemo02 extends JDialog {
        public MyDialogDemo02(){
            this.setResizable(false);
            this.setTitle("警告");
            this.setVisible(true);
            this.setBounds(380,150,100,80);

            Container container = this.getContentPane();
            //container.setLayout(null);

            //创建元素
            JLabel label01 = new JLabel("姓名不能为空");

            //布局
            JPanel panel01 = new JPanel(new GridLayout(1,1));
            label01.setBounds(30,30,80,50);
            panel01.setBounds(30,30,200,100);

            panel01.setBackground(Color.red);
            panel01.add(label01);
            container.add(panel01);
        }
    }

    //内部类，04 开始测温按钮事件
    class MyActionListener04 implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            if(username.equals("")){
                System.out.println("姓名不能为空");
                //姓名为空时的警告弹窗
                new MyDialogDemo02();
            }else if(userCode == null){
                System.out.println("请写了名字后，点生成击信息码按钮");
                //信息码为空时的警告弹窗
                new MyDialogDemo03();
            }else {
                tem = new UserTem().Number();
                new Insert().InsertTest(username,userCode,tem);
                //new Insert().InsertTest("mk","123456",40);
                User user = new User(username, userCode, tem);
                System.out.println(user);
                //展示测温者信息的弹窗
                new MyDialogDemo04();
            }
        }
    }

    //内部类，05 信息码为空时的警告弹窗
    class MyDialogDemo03 extends JDialog {
        public MyDialogDemo03(){
            this.setResizable(false);
            this.setTitle("警告");
            this.setVisible(true);
            this.setBounds(380,370,180,70);

            Container container = this.getContentPane();
            //container.setLayout(null);

            //创建元素
            JLabel label01 = new JLabel("请写了名字后，点击本按钮");

            //布局
            JPanel panel01 = new JPanel(new GridLayout(1,1));
            label01.setBounds(30,30,80,50);
            panel01.setBounds(30,30,200,100);

            panel01.setBackground(Color.red);
            panel01.add(label01);
            container.add(panel01);
        }
    }

    //内部类，06 展示测温者信息的弹窗
    class MyDialogDemo04 extends JDialog {
        public MyDialogDemo04(){
            this.setResizable(false);
            this.setTitle("当前测温者信息");
            this.setVisible(true);
            this.setBounds(230,470,250,120);

            Container container = this.getContentPane();
            //container.setLayout(null);

            //创建元素
            JLabel label01 = new JLabel("当前测温者姓名：" + username);
            JLabel label02 = new JLabel("当前测温者信息码：" + userCode);
            JLabel label03 = new JLabel("当前测温者体温：" + tem);
            //布局
            JPanel panel01 = new JPanel(new GridLayout(3,1));
            label01.setBounds(30,30,80,50);
            label02.setBounds(30,60,80,50);
            label03.setBounds(30,90,80,50);
            panel01.setBounds(30,30,200,100);

            panel01.setBackground(new Color(188, 242, 74));
            panel01.add(label01);
            panel01.add(label02);
            panel01.add(label03);
            container.add(panel01);
        }
    }
}